﻿using Kingdee.BOS;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Core.Validation;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.SerivesPlugin.Ronma
{
    public class UnAuditValidator : AbstractValidator
    {
        public override void Validate(ExtendedDataEntity[] dataEntities, ValidateContext validateContext, Context ctx)
        {
            Field field = validateContext.BusinessInfo.GetBillStatusField();
            Field billNoField = validateContext.BusinessInfo.GetBillNoField();
            foreach (var order in dataEntities)
            {
                string billStatus = Convert.ToString(field.GetFieldValue(order.DataEntity));
                string formId = validateContext.BusinessInfo.GetForm().Id;
                //获取ID
                string idKey = GetOaIdKey(formId);
                Field oafield = validateContext.BusinessInfo.GetField(idKey);
                int oaId = Convert.ToInt32(oafield.GetFieldValue(order.DataEntity));
                if (!billStatus.EqualsIgnoreCase("C") && oaId > 0)
                {
                    string billNo = Convert.ToString(billNoField.GetFieldValue(order.DataEntity));
                    //添加校验的错误信息
                    validateContext.AddError(order,
                        new ValidationErrorInfo(
                            field.Key, // 出错的字段Key，可以空
                            Convert.ToString(order.DataEntity["id"]), // 数据包内码，必填，后续操作会据此内码避开此数据包
                            order.DataEntityIndex, // 出错的数据包在全部数据包中的顺序
                            0, // 出错的数据行在全部数据行中的顺序，如果校验基于单据头，此为0
                            "RKUnAuditCheck", // 错误编码，可以任意设定一个字符，主要用于追查错误来源
                            "已经提交OA，不允许反审核！", // 错误的详细提示信息 
                            string.Format("反审核：{0}", billNo), // 错误的简明提示信息
                            ErrorLevel.Error)); // 错误级别：警告、错误...
                }
            }
        }

        private string GetOaIdKey(string formId)
        {
            string oaid = "FOAId";
            if (formId.EqualsIgnoreCase("PUR_POXChange"))
            {
                oaid = "FOAId2";
            }
            if (formId.EqualsIgnoreCase("PUR_ContractChange"))
            {
                oaid = "FOAId2";
            }
            if (formId.EqualsIgnoreCase("CRM_XContract"))
            {
                oaid = "FOAId2";
            }
            if (formId.EqualsIgnoreCase("SAL_XORDER"))
            {
                oaid = "FOAId2";
            }
            return oaid;
        }
    }
}
